package game.entity;



import game.helper.IAbility;
import game.helper.IMoving;
import game.helper.Input;
import game.shared.Constants;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

public class AbsSprite extends Sprite{

	int id;
	SpriteManager spriteMamanger;
	ArrayList<AbsAnimation> animations;
	protected int curAnimationIndex;
	IAbility iAbility;
	IMoving mover;
	Input input;
	
	public AbsSprite(AbsSprite absSprite){
		id = absSprite.getID();
		spriteMamanger = absSprite.spriteMamanger;
		animations = new ArrayList<AbsAnimation>();
		curAnimationIndex = absSprite.curAnimationIndex;
		iAbility = absSprite.iAbility;
		mover = absSprite.mover;
		input = absSprite.input;
		for(int i=0; i< absSprite.animations.size(); i++)
			animations.add(new AbsAnimation(absSprite.animations.get(i)));
	}
	
	public AbsSprite(int id, SpriteManager _SpriteManager, String animationImgPath, int idAnimation, float frameDuration, int frame_rows, int frame_cols) {
		this.id = id;
		spriteMamanger = _SpriteManager;
		animations = new ArrayList<AbsAnimation>();
		animations.add(new AbsAnimation(idAnimation,animationImgPath,frameDuration, frame_rows, frame_cols));
	}
	
	public AbsSprite(int id, SpriteManager _SpriteManager, String animationImgPath, int idAnimation, float frameDuration, int scrX, int scrY, int frameWidth, int frameHeight, int frame_rows, int frame_cols)
	{
		this.id = id;
		spriteMamanger = _SpriteManager;
		animations = new ArrayList<AbsAnimation>();
		animations.add(new AbsAnimation(idAnimation, animationImgPath, frameDuration, scrX, scrY, frameWidth, frameHeight, frame_rows, frame_cols));
	}
	
	public AbsSprite(int id, SpriteManager _SpriteManager, String configFilePath)
	{
		this.id = id;
		spriteMamanger = _SpriteManager;
		animations = new ArrayList<AbsAnimation>();
		LoadFileConfig(configFilePath);
	}
	
	private void LoadFileConfig(String configFilePath)
	{
		FileHandle file = Gdx.files.internal(configFilePath);
		String strCfg = file.readString();
		String[] lines = strCfg.split("\r\n");
		for(int i=0; i<lines.length; i++)
		{
			String[] values = lines[i].split(";");

			int aid = Integer.parseInt(values[0]);
			String path = values[1];
			float duration = Float.parseFloat(values[2]);
			int x = Integer.parseInt(values[3]);
			int y = Integer.parseInt(values[4]);
			int w = Integer.parseInt(values[5]);
			int h = Integer.parseInt(values[6]);
			int r = Integer.parseInt(values[7]);
			int c = Integer.parseInt(values[8]);
			Texture texture = new Texture(Gdx.files.internal(path));
			animations.add(new AbsAnimation(aid, texture, duration, x, y, w, h, r, c));
		}
	}

	public void render(SpriteBatch spriteBatch){
		animations.get(curAnimationIndex).draw(spriteBatch, this.getX(), this.getY());
	}
	
	public void renderShape(){
	}
	
	public void update()
	{
		if(mover != null)
			mover.run(this);
		//Touch vao window icon hoac sprite ben trai.
		if(input != null){
			if(input.touchDown(Gdx.input.getX(), Constants.SCREEN_HEIGHT - Gdx.input.getY(), 0, 0)){
				switch (this.getID()) {
				case 0:
					this.setState(Constants.SPRITE_ATTACK_RIGHT);
					break;
				case 1:
					this.setState(Constants.SPRITE_SHINE);
					break;
				}
			}
			else{
				switch (this.getID()) {
				case 0:
					this.setState(Constants.SPRITE_STAND_RIGHT);
					break;
				case 1:
					this.setState(Constants.SPRITE_WAITING);
					break;
				}
			}
		}
		
	}
	

	@Override
	public float getWidth() {
		return animations.get(curAnimationIndex).getDimension().x;
	}
	
	@Override
	public float getHeight() {
		return animations.get(curAnimationIndex).getDimension().y;
	}
	
	public int getID() 
	{
		return id;
	}
	
	public boolean setState(int stateID)
	{
		for(int i=0; i<animations.size(); i++)
		{
			int id = animations.get(i).getID();
			if(id == stateID)
			{
				this.curAnimationIndex = i;
				return true;
			}
		}
		return false;
	}
	
	public boolean haveState(int stateID)
	{
		for(int i=0; i<animations.size(); i++)
		{
			if(animations.get(i).getID() == stateID)
				return true;
		}
		return false;
	}
	public int getCurrentState()
	{
		return animations.get(curAnimationIndex).getID();
	}
	
	
	public void setMoving(IMoving mover)
	{
		this.mover = mover;
	}
	
	@Override
	public Rectangle getBoundingRectangle() {
		Vector2 wh = animations.get(curAnimationIndex).getDimension();
		return new Rectangle(this.getX(),this.getY(),wh.x,wh.y);
	}

	public float getSpeed() {
		return mover.getSpeed();
	}
	
	public void setInput(Input _input){
		this.input = _input;
	}
	public Input getInput(){
		return this.input;
	}
	
}
